
import { _decorator, Component, Node, CCInteger, Vec3, math, Animation, Collider } from 'cc';
import { BuilderControl } from './BuilderControl';
import { SpawnItemData } from './SpawnConfig';
const { ccclass, property } = _decorator;

@ccclass('ObstacleControl')
export class ObstacleControl extends BuilderControl {

    @property(CCInteger)
    gap = 0;
    x_pos = 0;


    //重载生成方法 
    createToEnd() {

        this.GetEndPos();

        // 

        console.log('this.startCreatePosZ ', this.startCreatePosZ)

        while (this.startCreatePosZ < this.endPosZ) {
            let num = math.randomRangeInt(0, 2)
            //每三个为一组 是纵向生成3个

            let spawnItem = this.doSpawnItem();
            //三个生成完了 更新 this.startCreatePosZ
            if (spawnItem) {
                this.startCreatePosZ += this.gap;
            }
        }

    }



    onSpawn(node: Node, spawnItemData: SpawnItemData) {
        let x = math.randomRangeInt(0, 3);
        if (x == 0) {
            this.x_pos = 0;
        } else if (x == 1) {
            this.x_pos = 1.25;
        } else if (x == 2) {
            this.x_pos = -1.25;
        }

        node.setPosition(new Vec3(this.x_pos, 0, this.startCreatePosZ));
        node.active = true;
        node.getComponent(Collider)!.enabled = true;

        node.getComponent(Animation)?.play('ObstacleIdle')
    }


}


